from typing import List


class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        # 计算前缀和、后缀和
        prefix = [0] * (len(nums) + 1)
        suffix = [0] * (len(nums) + 1)

        for i in range(len(nums)):
            prefix[i + 1] = prefix[i] + nums[i]
        for i in range(len(nums) - 1, -1, -1):
            suffix[i] = suffix[i + 1] + nums[i]

        # 计算结果
        for i in range(len(nums)):
            if prefix[i] == suffix[i + 1]:
                return i
        return -1


if __name__ == "__main__":
    print(Solution().pivotIndex(nums=[1, 7, 3, 6, 5, 6]))  # 3
    print(Solution().pivotIndex(nums=[1, 2, 3]))  # -1
    print(Solution().pivotIndex(nums=[2, 1, -1]))  # 0
